<title>所有渠道商</title>
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>渠道商管理</cite></a>
    <a><cite>所有渠道商</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
      <div class="layui-tab-content">
          <div class="layui-btn-group demoTable lay-mb15">
              <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
              <div class="layui-form-item" style="margin:0;">
                <div class="layui-inline">
                  <label class="layui-form-label">渠道商ID</label>
                  <div class="layui-input-block">
                  <input type="text" name="channelMchId" id="channelMchId" placeholder="请输入渠道商ID" autocomplete="off" class="layui-input">
                  </div>
                </div>

                <div class="layui-inline">
                 <!--<button id="search" class="layui-btn  layuiadmin-btn-admin" data-type="reload"><i class="layui-icon layui-icon-search"></i></button>-->
                  <button id="search" class="layui-btn layuiadmin-btn-admin" data-type="reload">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                  </button>
                </div>
              </div>
            </div>
          </div>
          <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
              <button id="addChannelMch" class="layui-btn layui-btn-normal" data-type="add"><i class="layui-icon"></i>添加
              </button>
              <button id= "output" class="layui-btn layui-btn-warm" data-type="output"><i class="layui-icon layui-icon-download-circle"></i>导出
              </button>
                <button id= "batchdel" class="layui-btn layui-btn-danger" data-type="batchdel"><i class="layui-icon"></i>删除
                </button>
            </div>
          <table id="XxPay_Mgr_ChannelMch_dataAll" lay-filter="XxPay_Mgr_ChannelMch_dataAll"></table>
        </div>
      </div>
    </div>
</div>
<style>
  .viewInfo {
    padding: 30px;
    font-size: 14px;
    color: #666;
  }

  .viewInfo li {
    margin-bottom: 15px;
  }

  .viewInfo li span {
    display: inline-block;
    width: 25%;
    text-align: right;
    margin-right: 30px;
    font-weight: bold;
  }


</style>
<script>
  layui.use(['admin', 'table', 'util'], function(){

//


    var $ = layui.$
            ,admin = layui.admin
            ,table = layui.table
            ,element = layui.element
            ,form = layui.form;

    element.render('breadcrumb', 'breadcrumb');

    var tplStatus = function(d){
      if(d.status == 1) {
        return '  <input type="checkbox" name="status" lay-filter="layTableSwitch" lay-skin="switch" lay-text="开|关" checked="true"> <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>开</em> <i></i></div>';

      }
        return ' <input type="checkbox" name="status" lay-filter="layTableSwitch" lay-skin="switch" lay-text="开|关" > <div class="layui-unselect layui-form-switch" lay-skin="_switch"><em>关</em><i></i></div>';

    };
    var tplEdit = function(d){
      return "<a  href="+ d.loginChannelMchUrl +" class='layui-btn layui-btn-small layui-btn-xs' target='_blank'>登录系统</a>" +
              "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='merchantDetail'>查看信息</a>" +
              "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='settConfig'>结算设置</a>" +
              "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='payPassage'>通道归属</a>" +
              "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='agentpayPassage'>代付通道</a>" +
              "<a class='layui-btn layui-btn-small layui-btn-xs' lay-event='change'>余额变更</a>" +
              /*"<a class='layui-btn layui-btn-primary layui-btn-xs' lay-event='pwdReset'>重置密码</a>" +*/
              "<a class='layui-btn layui-btn-xs' lay-event='edit'>编辑</a>";
    };
      var tplAccount = function(d){
          return  "<div style='background-color: #5FB878'>" +
              "<span style='color: gold'>账户余额:" + d.balance/100 +"元 &nbsp &nbsp</span>" +
              "<span style='color: white'>代付余额:" + d.agentpayBalance/100 +"元 &nbsp &nbsp</span>" +
              "<span style='color: red'>冻结金额:" + d.frozenMoney/100 +"元 &nbsp &nbsp</span>" +
              "<span style='color: brown'>保证金额:" + d.securityMoney/100 +"元 </span>" +
              "</div>"
      };

//      admin.req({
//          type: 'get',
//          url: layui.setter.baseUrl + '/account/get?mchId=' + mchId,
//          error: function(err){
//              layer.alert(err.msg,{title:'请求失败'})
//          },
//          success:function(res){
//              if(res.code == 0 && res.data != null){
//                 // $('#mchId').val(res.data.mchId);
//                  $('#amountTip').html(
//                      "账户余额: <span style='color: green'>" + res.data.balance / 100 + "</span> 元 " +
//                      "代付余额: <span style='color: green'>" + res.data.agentpayBalance / 100 + "</span> 元 " +
//                      "冻结金额: <span style='color: red'>" + res.data.frozenMoney / 100 + "</span> 元 " +
//                      "保证金额: <span style='color: red'>" + res.data.securityMoney / 100 + "</span> 元"
//                  );
//              }
//          }
//      });

    //商户列表
    table.render({
      elem: '#XxPay_Mgr_ChannelMch_dataAll'
      ,url: layui.setter.baseUrl + '/channelmch_info/list' //列表接口
      ,where: {
          access_token: layui.data(layui.setter.tableName).access_token
      }
      ,id: 'tableReload'
      ,page: true
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field: 'channelMchId', fixed: 'left',title: '渠道商ID', width: 100}
        ,{field: 'userName', title: '登录账户', width: 120}
        ,{field: 'channelMchName', title: '渠道商名称', width: 120}
        ,{field: 'supChannelMchAgentId', title: '上级渠道代理ID', width: 130}
            ,{field: 'status', title: '状态', width: 80,templet: tplStatus}
        ,{field: 'account', title: '账户总额', width: 600, templet: tplAccount}
        /*,{field: 'createTime', title: '时间', width: 200, templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}*/
        ,{field: 'edit', title: '操作', width: 500, templet: tplEdit }
      ]]
      ,skin: 'line'
    });

    //监听工具条
    table.on('tool(XxPay_Mgr_ChannelMch_dataAll)', function(obj){
      var data = obj.data;
      if(obj.event === 'merchantDetail'){
          admin.req({
              type: 'get',
              url: layui.setter.baseUrl + '/channelmch_info/get',
              data: {
                  channelMchId : data.channelMchId
              },
              error: function(err){
                  layer.alert(JSON.stringify(err.field), {
                      title: '系统异常'
                  })
              },
             // layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
              success: function(res){
                  if(res.code == 0){
                      layer.open({
                          title: '查看渠道信息',
                          type: 1,
                          //skin: 'layui-layer-rim',
                          shadeClose: true,
                          area: admin.screen() < 2 ? ['80%', '300px'] : ['500px', '350px'],
                          content: '<div id="view">'
                            +'<ul class="viewInfo">'
                            +'<li><span>渠道商ID：</span>'
                            + res.data.channelMchId
                            + '</li>'
                            +'<li><span>上级代理渠道ID：</span>'
                            + res.data.supChannelMchAgentId
                            + '</li>'
                            +'<li><span>渠道商名称：</span>'
                            + res.data.channelMchName
                            + '</li>'
                            +'<li><span>登录账户：</span>'
                            + res.data.userName
                            + '</li>'
                            +'<li><span>联系方式：</span>'
                            + res.data.mobile
                            + '</li>'
                            +'<li><span>邮箱：</span>'
                            + res.data.email
                            + '</li>'
                            +'<li><span>qq：</span>'
                            + res.data.qq
                            + '</li>'
                            +'</ul>'
                          +'</div>'
                      });
                  }
              }
          });
      //  location.href = layui.setter.baseLocal + "merchant/list/mch_view/mchId=" + data.mchId;
      } else if(obj.event === 'accountDetail'){
        location.href = layui.setter.baseLocal + "merchant/list/account_view/mchId=" + data.mchId;
      } else if(obj.event === 'payPassage'){
//        if(data.type == 2) {
//          layer.msg('私有账户,无需设置!');
//          return;
//        }
//        location.href = layui.setter.baseLocal + "merchant/pay_passage/list/mchId=" + data.mchId;
          layer.open({
              title: '分配渠道商通道',
              type: 2,
              //skin: 'layui-layer-rim',
              shadeClose: false,
              area: admin.screen() < 2 ? ['80%', '300px'] : ['800px', '600px'],
              content: layui.setter.base + 'views/channelmch/list/channel_add.html'
//              content: layui.setter.baseUrl + '/channelmch_passage/getPayPassageList',
          });

      } else if(obj.event === 'agentpayPassage'){
        if(data.type == 2) {
          layer.msg('私有账户,无需设置!');
          return;
        }
        location.href = layui.setter.baseLocal + "merchant/agentpay_passage/list/mchId=" + data.mchId;
      } else if(obj.event === 'settConfig'){
        if(data.type == 2) {
          layer.msg('私有账户,无需设置!');
          return;
        }
        location.href = layui.setter.baseLocal + "merchant/list/mch_sett/mchId=" + data.mchId;
      }else if(obj.event === 'pwdReset'){
        layer.prompt({
          formType: 1,
          title: '请输入新密码'
        }, function(value, index, elem){
          admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/mch_info/pwd_reset',
            data: {
              mchId : data.mchId,
              password : value
            },
            error: function(err){
              layer.alert(JSON.stringify(err.field), {
                title: '错误提示'
              })
            },
            success: function(res){
              if(res.code == 0){
                layer.msg('修改成功', {icon: 1});
              }
            }
          });
          layer.close(index);
        });
      }else if(obj.event === 'change'){
        location.href = layui.setter.baseLocal + "merchant/list/mch_change/mchId=" + data.mchId;
      }else if(obj.event === 'edit'){
      //    http://localhost:8193/x_mgr/src/views/merchant/list/mch_edit.html?v=1.0.0
        //  /x_mgr/start/index.html#/merchant/list/mch_edit/mchId=20000006
          // '/x_mgr/start/index.html#/'
       //  alert(layui.setter.baseLocal + "merchant/list/mch_edit/mchId=" + data.mchId);
//          layer.open({
//              title: '新增',
//              type: 2,
//              skin: 'layui-layer-rim',
//              shadeClose: false,
//              area: admin.screen() < 2 ? ['80% ', '300px'] : ['800px', '600px'],
//              content: layui.setter.baseUrl + "merchant/list/mch_edit.html/mchId=" + data.mchId
//
////                    content: '<div id="addContent"></div>'
//          });
      location.href = layui.setter.baseLocal + "merchant/list/mch_edit/mchId=" + data.mchId;
      }

    });

    form.on("switch(layTableSwitch)",function(data){
      //  layer.msg(this.checked ? "true" : "false");

        var index_sms;
        // 获取当前控件
        var selectIfKey=data.othis;
        // 获取当前所在行
        var parentTr = selectIfKey.parents("tr");
        // 获取当前所在行的索引
        var parentTrIndex = parentTr.attr("data-index");
        var channelMchId=parentTr.find(('td:eq(1)')).text().trim();
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/channelmch_info/updateStatus',
            data: {
                channelMchId : channelMchId,
                status : this.checked ? "true" : "false"
            },
            beforeSend:function(){
                index_sms = layer.msg('正在切换中，请稍候',{icon: 16,time:false,shade:0.8});
            },
            error: function(err){
                layer.alert(JSON.stringify(err.field), {
                    title: '错误提示'
                })
            },
            success: function(res){
                if(res.code == 0){
                    layer.close(index_sms);
                    layer.msg('修改渠道商状态成功', {icon: 6});
                }
            }
        });
 //       layer.msg(this.checked ? "true" : "false");

     // layer.msg( data.channelMchId);
    })
    // 搜索
//    var $ = layui.$, active = {
//      reload: function(){
//          alert(1);
//        var channelMchId = $('#channelMchId').val();
//        //执行重载
//        table.reload('tableReload', {
//          page: {
//            curr: 1 //重新从第 1 页开始
//          }
//          ,where: {
//                channelMchId: channelMchId
//          }
//        });
//      }
//    };
    $('#search').on('click', function() {
        var channelMchId = $('#channelMchId').val();
      //  alert(channelMchId);
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

      $('#addChannelMch').on('click', function() {

          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });
      $('#batchdel').on('click', function() {

          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });

      var active = {
          reload: function(){
              var channelMchId = $('#channelMchId').val();
             //执行重载
            table.reload('tableReload', {
              page: {
                curr: 1 //重新从第 1 页开始
              }
              ,where: {
                    channelMchId: channelMchId
              }
            });
         },
          batchdel: function(){
              var checkStatus = table.checkStatus('tableReload')
                  ,checkData = checkStatus.data; //得到选中的数据
              if(checkData.length === 0){
                  return layer.msg('请选择数据');
              }
              layer.confirm('确定删除选中的渠道商吗？', function(){
                  var channelMchIds = "";
                  $.each(checkData, function(i, n){
                      channelMchIds = channelMchIds + "," + n.channelMchId;
                  });
                  if(channelMchIds != "") {
                      channelMchIds = channelMchIds.substring(1);
                  }

                  admin.req({
                      type: 'get',
                      url: layui.setter.baseUrl + '/channelmch_info/delete',
                      data: {
                          channelMchIds: channelMchIds,
                      },
                      success: function(res){
                          if(res.code == 0) {
                              layer.msg('删除成功', {
                                  icon: 1
                              });
                              table.reload("tableReload");
                          }
                      }
                  });
              });
          },
          getCheckData: function () { //获取选中数据
              var checkStatus = table.checkStatus('tableReload')
                  , data = checkStatus.data;
              active.getId(data)

              layer.alert(JSON.stringify(data));
          }
          , getId: function (data) {
              var idArr = [];
              for (var i = 0; i < data.length; i++) {
                  console.log(data[i].id)
                  idArr.push(data[i].id)
              }
              console.log(idArr)
          }
          , getCheckLength: function () { //获取选中数目
              var checkStatus = table.checkStatus('tableReload')
                  , data = checkStatus.data;
              layer.msg('选中了：' + data.length + ' 个');
          }
          , isAll: function () { //验证是否全选
              var checkStatus = table.checkStatus('tableReload');
              layer.msg(checkStatus.isAll ? '全选' : '未全选')
          }
          , add: function() { // 增加一条列表
              layer.open({
                  title: '新增渠道商',
                  type: 2,
                  //skin: 'layui-layer-rim',
                  shadeClose: false,
                  area: admin.screen() < 2 ? ['80%', '300px'] : ['800px', '600px'],
                  content: layui.setter.base + 'views/channelmch/list/mch_add.html'
              });
          }
      };
      // 渲染表单
    form.render();
  });
</script>